package com.example.yqand4v1_1.controller;

import com.example.yqand4v1_1.beans.HttpResponseEntity;
import com.example.yqand4v1_1.dao.entity.Team;
import com.example.yqand4v1_1.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/team")
public class TeamController {
    @Autowired
    private TeamService teamService;
    @RequestMapping(value = "/queryTeamsByAmount",method = RequestMethod.GET,headers = "Accept=application/json")
    public HttpResponseEntity queryTeamsByAmount(@RequestParam("count")int count, @RequestParam("amount")int amount)
    {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        List<Team> teams=teamService.queryTeamsByAmount(count,amount);
        if(CollectionUtils.isEmpty(teams))
        {
            httpResponseEntity.setCode("204");
            httpResponseEntity.setMessage("无团队信息");
        }else {
            httpResponseEntity.setCode("200");
            httpResponseEntity.setMessage("成功查询");
            httpResponseEntity.setData(teams);
        }
        return httpResponseEntity;
    }
    @RequestMapping(value = "/createTeam",method = RequestMethod.POST,headers = "Accept=application/json")
    public HttpResponseEntity createTeam(@RequestBody Team team)
    {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        int result=teamService.addTeam(team);
        if(result!=1)
        {
            httpResponseEntity.setCode("204");
            httpResponseEntity.setMessage("未创建成功");
        }else {
            httpResponseEntity.setCode("200");
            httpResponseEntity.setMessage("成功查询");
        }
        return httpResponseEntity;
    }
    @RequestMapping(value = "/querySingleTeamById",method = RequestMethod.GET,headers = "Accept=application/json")
    public HttpResponseEntity querySingleTeamById(@RequestParam("teamId")String param)
    {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        int teamId=Integer.parseInt(param);
        Team team=teamService.querySingleTeamById(teamId);
        if(team==null)
        {
            httpResponseEntity.setCode("204");
            httpResponseEntity.setMessage("无团队信息");
        }else {
            httpResponseEntity.setCode("200");
            httpResponseEntity.setMessage("成功查询");
            httpResponseEntity.setData(team);
        }
        return httpResponseEntity;
    }
    @RequestMapping(value = "/queryTeamOfVolunteer",method = RequestMethod.GET,headers = "Accept=application/json")
    public HttpResponseEntity queryTeamOfVolunteer(@RequestParam("volunteerId") int volunteerId)
    {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        Team team=teamService.queryTeamOfVolunteer(volunteerId);
        if(team==null)
        {
            httpResponseEntity.setCode("204");
            httpResponseEntity.setMessage("无团队信息");
        }else {
            httpResponseEntity.setCode("200");
            httpResponseEntity.setMessage("成功查询");
            httpResponseEntity.setData(team);
        }
        return httpResponseEntity;
    }
    @RequestMapping(value = "/joinTeam",method = RequestMethod.POST,headers = "Accept=application/json")
    public HttpResponseEntity joinTeam(@RequestBody Map<String,Object> map)
    {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        int teamId=Integer.parseInt(map.get("teamId").toString());
        int volunteerId=Integer.parseInt(map.get("volunteerId").toString());
        int result=teamService.participateTeam(teamId,volunteerId);
        if(result==0)
        {
            httpResponseEntity.setCode("204");
            httpResponseEntity.setMessage("报名失败");
        }else {
            httpResponseEntity.setCode("200");
            httpResponseEntity.setMessage("报名成功");
        }
        return httpResponseEntity;
    }
    @RequestMapping(value = "/queryNameAndPhoneNumOfLeader",method = RequestMethod.GET,headers = "Accept=application/json")
    public HttpResponseEntity queryNameAndPhoneNumOfLeader(@RequestParam("leaderId") int leaderId)
    {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        Map<String,String> result=teamService.queryNameAndPhoneNumOfLeader(leaderId);
        if(CollectionUtils.isEmpty(result))
        {
            httpResponseEntity.setCode("204");
            httpResponseEntity.setMessage("查询失败");
        }else {
            httpResponseEntity.setCode("200");
            httpResponseEntity.setMessage("查询成功");
            httpResponseEntity.setData(result);
        }
        return httpResponseEntity;
    }
    @RequestMapping(value = "/queryTotalNumOfTeams",method = RequestMethod.GET,headers = "Accept=application/json")
    public HttpResponseEntity queryTotalNumOfTeams()
    {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        int result=teamService.queryTotalNumOfTeams();
        if(result==0)
        {
            httpResponseEntity.setCode("204");
            httpResponseEntity.setMessage("无团队");
        }else {
            httpResponseEntity.setCode("200");
            httpResponseEntity.setMessage("成功查询");
            httpResponseEntity.setData(result);
        }
        return httpResponseEntity;
    }
}
